/*
 * Copyright (C) 2004, Velocityme Partnership
 * StateMachineActionForm.java
 *
 * Created on 19 October 2004, 06:26
 */

package com.velocityme.www.actionforms;

import com.velocityme.valueobjects.*;
import com.velocityme.www.dto.IntegerString;
import java.io.Serializable;
import java.util.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class StateMachineActionForm extends ActionForm {
    
    public class Transition implements Serializable {
        private Integer m_transitionId;
        private Integer m_sourceStateId;
        private Integer m_destinationStateId;
        private Integer m_statusId;
        private Boolean m_isOwnerResponsible = Boolean.FALSE;
        private Boolean m_isUserResponsible = Boolean.FALSE;
        private Integer[] m_assignedRoleIds;
            
        public Integer getTransitionId() { return m_transitionId; }
        public void setTransitionId(Integer transitionId) { m_transitionId = transitionId; }
        public Integer getSourceStateId() { return m_sourceStateId; }
        public void setSourceStateId(Integer sourceStateId) { m_sourceStateId = sourceStateId; }
        public Integer getDestinationStateId() { return m_destinationStateId; }
        public void setDestinationStateId(Integer destinationStateId) { m_destinationStateId = destinationStateId; }
        public Integer getStatusId() { return m_statusId; }
        public void setStatusId(Integer statusId) { m_statusId = statusId; }
        public Boolean getIsOwnerResponsible() { return m_isOwnerResponsible; }
        public void setIsOwnerResponsible(Boolean isOwnerResponsible) { m_isOwnerResponsible = isOwnerResponsible; }
        public Boolean getIsUserResponsible() { return m_isUserResponsible; }
        public void setIsUserResponsible(Boolean isUserResponsible) { m_isUserResponsible = isUserResponsible; }
        public Integer[] getAssignedRoleIds() { return m_assignedRoleIds; }
        public void setAssignedRoleIds(Integer[] assignedRoleIds) { m_assignedRoleIds = assignedRoleIds; }    
    }
    
    private ArrayList m_transitionList;
    private Integer m_taskStateMachineId;
    private int m_selectedValue = 0;
    private String m_cmd="";
    
    /** Creates a new instance of StateMachineActionForm */
    public StateMachineActionForm() {
        m_transitionList = new ArrayList();
    }
    
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        Iterator i = m_transitionList.iterator();
        while(i.hasNext()) {
            Transition transition = (Transition)i.next();
            transition.setIsOwnerResponsible(Boolean.FALSE);
            transition.setIsUserResponsible(Boolean.FALSE);
            transition.setAssignedRoleIds(new Integer[0]);
        }
    }
    
    public void setCmd(int index, String value) {
        m_selectedValue = index;
        m_cmd = value;
    }
    
    public String getCmd(int index) {
        return m_cmd;
    }
    
    public String getCmd() {
        return m_cmd;
    }
    
    public int getIndex() {
        return m_selectedValue;
    }
    
    public void setTransitionValues(TransitionValue[] transitionValues, Collection allRolePairs) {
        Map rolePairMap = new HashMap();
        Iterator k = allRolePairs.iterator();
        while(k.hasNext()) {
            IntegerString pair = (IntegerString)k.next();
            rolePairMap.put(pair.getInteger(), pair);
        }
        m_transitionList.clear();
        for (int i = 0; i < transitionValues.length; i++) {
            TransitionValue transitionValue = transitionValues[i];
            Transition transition = new Transition();
            transition.m_transitionId = transitionValue.getTransitionId();
            transition.m_sourceStateId = transitionValue.getSourceStateValue().getStateId();
            transition.m_destinationStateId = transitionValue.getDestinationStateValue().getStateId();
            transition.m_statusId = transitionValue.getStatusValue().getStatusId();
            transition.m_isOwnerResponsible = transitionValue.getIsOwnerResponsible();
            transition.m_isUserResponsible = transitionValue.getIsUserResponsible();
            transition.m_assignedRoleIds = new Integer[transitionValue.getResponsibleRoleValues().length];
            for (int j = 0; j < transitionValue.getResponsibleRoleValues().length; j++) {
                transition.m_assignedRoleIds[j] = transitionValue.getResponsibleRoleValues()[j].getRoleId();
            }
            m_transitionList.add(transition);
        }
    }
    
    public TransitionValue[] getTransitionValues(StateValue[] stateValues, StatusValue[] statusValues, RoleValue[] roleValues) {
        Map stateValueMap = new HashMap();
        for (int i = 0; i < stateValues.length; i++) {
            stateValueMap.put(stateValues[i].getStateId(), stateValues[i]);
        }
        Map statusValueMap = new HashMap();
        for (int i = 0; i < statusValues.length; i++) {
            statusValueMap.put(statusValues[i].getStatusId(), statusValues[i]);
        }
        Map roleValueMap = new HashMap();
        for (int i = 0; i < roleValues.length; i++) {
            roleValueMap.put(roleValues[i].getRoleId(), roleValues[i]);
        }
        TransitionValue[] transitionValues = new TransitionValue[m_transitionList.size()];
        Iterator i = m_transitionList.iterator();
        int index = 0;
        while(i.hasNext()) {
            Transition transition = (Transition)i.next();
            TransitionValue transitionValue = new TransitionValue();
            if (transition.getTransitionId() != null)
                transitionValue.setTransitionId(transition.getTransitionId());
            transitionValue.setSourceStateValue((StateValue)stateValueMap.get(transition.getSourceStateId()));
            transitionValue.setDestinationStateValue((StateValue)stateValueMap.get(transition.getDestinationStateId()));
            transitionValue.setStatusValue((StatusValue)statusValueMap.get(transition.getStatusId()));
            transitionValue.setIsOwnerResponsible(transition.getIsOwnerResponsible());
            transitionValue.setIsUserResponsible(transition.getIsUserResponsible());
            RoleValue[] assignedRoleValues = new RoleValue[transition.getAssignedRoleIds().length];
            for (int j = 0; j < transition.getAssignedRoleIds().length; j++) {
                assignedRoleValues[j] = (RoleValue)roleValueMap.get(transition.getAssignedRoleIds()[j]);
            }
            transitionValue.setResponsibleRoleValues(assignedRoleValues);
            transitionValues[index++] = transitionValue;
        }
        return transitionValues;
    }
    
    public boolean processCmd(Collection allRolePairs) {
        if (m_cmd.equals("Add")) {
            Transition transition = new Transition();
            m_transitionList.add(transition);
            return true;
        }
        if (m_cmd.equals("Delete")) {
            m_transitionList.remove(m_selectedValue);
            return true;
        }
        return false;
    }
    
    public ArrayList getTransitionList() {
        return m_transitionList;
    }
    
    public void setTransitionList(ArrayList transitionList) {
        m_transitionList = transitionList;
    }
    
    public Integer getTaskStateMachineId() {
        return m_taskStateMachineId;
    }
    
    public void setTaskStateMachineId(Integer taskStateMachineId) {
        m_taskStateMachineId = taskStateMachineId;
    }
    
}
